﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Taurus.Platform.DTO;
using Taurus.Platform.BLL;
using Taurus.Parking.BLL;
using Taurus.Parking.DTO;
using Taurus.Parking.XML;
using Taurus.Platform.XML;
using Newtonsoft.Json.Linq;

namespace Taurus.Parking.Game
{
    public partial class GameStart : UserControl
    {

        #region construction
        public GameStart(Components.Component component)
        {
            InitializeComponent();
            dgvMyCars.AutoGenerateColumns = false;
            dgvMyParks.AutoGenerateColumns = false;
            _component = component;
        }
        #endregion

        #region field

        private AccountBll _account = new AccountBll();
        private IPersistence<ParkingUser> _per = new ParkingUserPersistence();
        private Components.Component _component;

        #endregion

        #region event

        private void cbxAccount_SelectedIndexChanged(object sender, EventArgs e)
        {
            MyDataBind(false);
        }

        private void GameStart_Load(object sender, EventArgs e)
        {
            cbxAccount.DataSource = _account.GetUserAccount();
            cbxAccount.DisplayMember = "Name";
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            //贴条
            for (int i = 0; i < dgvMyParks.SelectedRows.Count; i++)
            {
                Sticker(dgvMyParks.SelectedRows[i]);
            }
            MyDataBind(true);
        }

        private void btnParking_Click(object sender, EventArgs e)
        {
            //停车
            if (dgvMyCars.SelectedRows.Count!=0)
            {
                btnParking.Enabled = false;
                backgroundWorkerParking.RunWorkerAsync(cbxAccount.SelectedItem);
            }
            else
            {
                MessageBox.Show("没有选中要停车辆");
            }
        }
        private void backgroundWorkerParking_DoWork(object sender, DoWorkEventArgs e)
        {
            UserAccount ua = (UserAccount)e.Argument;
            for (int i = 0; i < dgvMyCars.SelectedRows.Count; i++)
            {
                Parking(dgvMyCars.SelectedRows[i], ua);
            }
        }

        private void backgroundWorkerParking_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            _component.SendMessage(string.Empty);
            btnParking.Enabled = true;
            MyDataBind(true);
        }

        private void kryptonCheckButton1_Click(object sender, EventArgs e)
        {
            if (cbtnSelectAllCar.Checked)
            {
                dgvMyCars.SelectAll();
                dgvMyParks.SelectAll();
            }
        }

        private void backgroundWorkerUpdateUserInfo_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            ShowInfo((MyGarage)e.Result);
        }

        private void backgroundWorkerUpdateUserInfo_DoWork(object sender, DoWorkEventArgs e)
        {
            MyGarage mg = (MyGarage)e.Argument;
            mg.UpdateMyData();
            e.Result = mg;
            _component.SendMessage(string.Empty);
        }

        #endregion

        //停车
        private void Parking(DataGridViewRow row, UserAccount ua)
        {
            string carid = row.DataBoundItem.GetType().GetProperty("CarID").GetValue(row.DataBoundItem, null).ToString();
            string name = row.DataBoundItem.GetType().GetProperty("Name").GetValue(row.DataBoundItem, null).ToString();
            _component.SendMessage("正在停" + name);

            JObject jo = InitSys.Instance.GetMyGarage(ua.UID).Parking(carid);
            if (jo != null)
            {
                int cash = (int)jo["cash"];
                int errno = (int)jo["errno"];
                string msg = (string)jo["error"];
            //    MessageBox.Show(string.Format("{0}{1}获得现金{2}", name, msg, cash));
            }
            else
            {
                MessageBox.Show("车位已经停满");
            }
        }
        //贴条
        private void Sticker(DataGridViewRow row)
        {
            string parkid = row.DataBoundItem.GetType().GetProperty("ParkID").GetValue(row.DataBoundItem, null).ToString();
            UserAccount ua = (UserAccount)cbxAccount.SelectedItem;
            MyGarage mg = InitSys.Instance.GetMyGarage(ua.UID);
            JObject jo = mg.Sticker(parkid);
            MessageBox.Show(jo["error"].Value<string>());
        }

        private void MyDataBind(bool isUpdate)
        {
            UserAccount ua = (UserAccount)cbxAccount.SelectedItem;
            MyGarage mg = InitSys.Instance.GetMyGarage(ua.UID);
            if (isUpdate)
            {
                _component.SendMessage(string.Format("更新{0}用户信息", ua.Name));
                backgroundWorkerUpdateUserInfo.RunWorkerAsync(mg);               
            }
            else
            {
                ShowInfo(mg);
            }
            
        }

        private void ShowInfo(MyGarage mg)
        {
            var myCars = from car in mg.MyAccount.Garage.MyCars select new { Name = car.Name, WhoPark = car.Park.OwnerName, Profit = car.Park.Profit, CarID = car.ID };
            dgvMyCars.DataSource = myCars.ToList();

            var myParks = from park in mg.MyAccount.Garage.MyParks select new { Name = park.Car.Name, OwnerName = park.Car.OwnerName, Profit = park.Profit, ParkID = park.ID };
            dgvMyParks.DataSource = myParks.ToList();

            lblCash.Text = mg.MyAccount.Cash + "元";
            if (cbtnSelectAllCar.Checked)
            {
                dgvMyCars.SelectAll();
                dgvMyParks.SelectAll();
            }
        }

        

        
       
    }
}
